/*
 * @Date: 2020-08-24 09:38:18
 * @LastEditors: xujianxing
 * @LastEditTime: 2020-08-26 15:27:50
 * @FilePath: \nili\src\modules\user\user.contoller.ts
 */

import {
  Controller,
  Get,
  Query,
  HttpCode,
  Header,
  Redirect,
  Param,
  HostParam,
  Post,
  Body,
  HttpException,
  HttpStatus,
  UseFilters,
  UsePipes,
  Ip,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { UserService } from './user.service';
import { IUser } from './interface/user';
import { HttpExceptionFilter } from 'src/common/filters/http-exception.filter';
import { UserDto } from './dto/create-user.dto';
import { ValidationPipe } from 'src/common/pipe/validation.pipe';
import { ParseIntPipe } from 'src/common/pipe/parse-int.pipe';
import { AuthGuard } from 'src/common/guard/auth.guard';
import { RolesGuard } from 'src/common/guard/role.guard';
import { Roles } from 'src/common/anotation/roles.ano';
import { LoggingInterceptor } from 'src/common/interceptor/logging.interceptor';
import { ErrorsInterceptor } from 'src/common/interceptor/exception.interceptor';
import { cacheInterceptor } from 'src/common/interceptor/cache.interceptor';
import { TimeoutInterceptor } from 'src/common/interceptor/timeout.interceptor';
import { TimeoutError } from 'rxjs';
import { Token } from 'src/common/anotation/token.ano';
import { User } from 'src/common/anotation/user.ano';

// @UseFilters(new HttpExceptionFilter())
// @UseGuards(RolesGuard)
// @UseInterceptors(LoggingInterceptor) //拦截器
@Controller({ /** host: 'localhost'  指定从哪个url来的请求*/ path: '/user' })
export class UserController {
  constructor(private readonly userService: UserService) {}

  // @Redirect('https://baidu.com',302) // 重定向
  // @HttpCode(200) // 自定义状态码
  // @UseFilters(new HttpExceptionFilter()) // 引用过滤器
  @Get('/findAll') /** 接口地址 */
  @Header('Cache-Control', 'no-store') /** 自定义响应头 */
  findAll(@Query() { page, size, version }): IUser[] {
    /** 拿到 Query 参数 ?page=1&size=12 */
    /** 通过返回一个对象，确定动态确定重定向的地址 */
    // return { url: 'http://baidu.com', statusCode: 200 };

    /** 假设此处发生了异常 */
    // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
    // throw new HttpException(
    //   {
    //     status: HttpStatus.NOT_FOUND,
    //     message: '没找到路径',
    //   },
    //   404,
    // );

    return this.userService.findAll(12, 12);
  }

  @Get('/detail/:id') // Param 形式参数 /user/99 , 利用 new ParseIntPipe() 管道转换参数类型为 Int
  findById(@Param('id', new ParseIntPipe()) id: number, @Token() token): IUser {
    console.log('decrator token: ', token);
    return this.userService.findById(id);
  }

  /**
   *  异步响应
   *
   *  @Get('/asyncFindAll')
   *  asyncFindAll(): Observable<string[]> {
   *   return of(['onw', 'two']);
   *  }
   *
   *   or
   *
   *  @Get('asyncFindAll')
   *  asyncFindAll(): Promise(string[]) {
   *    return ['onw', 'two']
   *  }
   */

  @Post('/create')
  @Roles('admin')
  // @UsePipes(ValidationPipe)
  // @UseInterceptors(ErrorsInterceptor)
  // @UseInterceptors(cacheInterceptor)
  @UseInterceptors(TimeoutInterceptor)
  async create(@Body() body: UserDto, @Ip() ip, @User('name') user) {
    console.log('user:', user);
    console.log('ip:', ip);
    throw new TimeoutError();
    return this.userService.create(user);
  }
}
